package com.ifast.integral.controller;

import java.util.Arrays;
import java.util.Optional;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.annotation.OperateRecord;
import com.ifast.common.base.AdminBaseController;
import com.ifast.common.service.DictService;
import com.ifast.common.utils.Result;
import com.ifast.integral.domain.IntegralExplain;
import com.ifast.integral.service.IntegralExplainService;
import com.ifast.sys.filter.SameUrlData;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 积分配置
 * @author ShiQiang
 * @date 2019年4月9日10:05:14
 */
@RequestMapping("/integral/explain/")
@Controller
public class IntegralExplainController extends AdminBaseController {
	
    private String preHtml = "lucky/integral/explain/";
    @Autowired
    private IntegralExplainService integralExplainService;
    @Autowired
    private DictService sysDictService;
    
    
    @GetMapping("/qyList") 
    @RequiresPermissions("integral:explain:qyList")
    String qyList(Model model) {
        return preHtml + "qy_list";
    }
    
    
    @GetMapping("qyListData") 
    @OperateRecord(OperateName = "列表",ModellName ="积分说明模块")
    @ResponseBody 
    @Log("获取积分说明列表")
    public Result<Page<IntegralExplain>> qyListData(Model model,IntegralExplain explain) {
         
         Page<IntegralExplain> page = this.integralExplainService.selectPage(getPage(IntegralExplain.class),new EntityWrapper<IntegralExplain>()
        		.eq("DW_CODE", this.getUser().getDwCode())
        		.eq(StrUtil.isNotBlank(explain.getClassify()),"classify",explain.getClassify())
        		.like(StringUtils.isNotBlank(explain.getQuestion()),"question",explain.getQuestion())
        		.orderBy("sort",false).orderBy("create_time"));
       
        Optional.ofNullable(page.getRecords()).ifPresent(examList -> 
		examList.stream().forEach(version ->Optional.ofNullable(sysDictService.selectById(version.getClassify()))
				.ifPresent( dictSys-> version.setClassify(dictSys.getName()))));
     
        return Result.ok(page);
    }
    
    /**
     *  
     * @param model
     * @param id 
     */
    @GetMapping("/add")
    @RequiresPermissions("integral:explain:add")
    public String add(Model model, String id){ 
        return  preHtml+"add";
    }
    /**
     * 编辑
     * @param model
     * @param id 
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("integral:explain:edit")
    public String edit(Model model,@PathVariable("id") String id){
    	model.addAttribute("info",integralExplainService.selectById(id));
        return  preHtml+"edit";
    } 
    /**
     * 查看
     * @param model
     * @param id 
     */
    @GetMapping("/view/{id}")
    @RequiresPermissions("integral:explain:edit")
    public String view(Model model,@PathVariable("id") String id){
    	model.addAttribute("info",integralExplainService.selectById(id));
        return  preHtml+"view";
    }
    /**
     * 删除
     * @param model
     * @param id 
     */
    @PostMapping("/remove")
    @ResponseBody
    @Log("删除积分说明")
    @RequiresPermissions("integral:explain:remove")
    public Result<?> remove(Model model,String id){ 
        return  integralExplainService.deleteById(id)?Result.ok():Result.fail();
    } 
    
    /**
     * 批量删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("integral:explain:batchRemove")
    @Log("批量删除积分说明")
    public Result<String> batchRemove(@RequestParam("ids[]") String[] deptIds) { 
        return integralExplainService.deleteBatchIds(Arrays.asList(deptIds))?Result.ok():Result.fail();
    }

    /**
     * 保存积分配置
     * @param integral
     */
    @PostMapping("/save")
    @ResponseBody
    @SameUrlData
    @OperateRecord(OperateName = "保存积分说明",ModellName ="积分说明模块" )
    public Result<?> saveSubject(IntegralExplain explain) {
        try{ 
        	return integralExplainService.save(explain.setDwCode(this.getUser().getDwCode()));
        }catch (Exception e){
            log.error(e.toString(),e);
            return Result.fail();
        } 
    } 
}